var addWishList = function(){
	var id = $(this).attr('rel');
	
	$.post(baseurl + '/product/addwishlist', {
		id:id
	},
	function(data){
		var mess;
		if(data == -1){
			mess = 'Sản phẩm này đã có trong danh sách yêu thích của bạn!';
		}
		else if(data ==-2){
			mess = "Vui lòng <a href='"+LOGIN_URL+"'><strong>đăng nhập</strong></a> để sử dụng chức năng này!";
		}
		else{
			mess = 'Bạn đã thêm vào danh sách yêu thích thành công!';
		}
	  
		$(this).myBoxy (Boxy,{
			message: mess
		});
	});
}


var addBlacklist= function(me_id, topic_id)
{
    var  blacklist_type = $("#blacklist_type").val();
    
    $.post(baseurl + '/product/addblacklist', {
        topic_id:topic_id,
        me_id: me_id,
        blacklist_type: blacklist_type
    },
    function(data){
        var mess;
        if(data == -1){
            mess = 'Bạn đã đưa vào báo xấu sản phẩm này rồi!';
        }
        else if(data ==-2){
            mess = 'Vui lòng đăng nhập để sử dụng chức năng này!';
        }
        else if(data ==-3){
            mess = 'Tham số truyền vào không đúng!';
        }
        else{
            mess = 'Bạn đã thêm vào danh sách báo xấu thành công!';
        }
	  
        $(this).myBoxy (Boxy,{
            message: mess
        });
    });
};

$(function(){
	/*Scroll to Top*/
	$("#toTop").scrollToTop({speed:1000,ease:"easeInBack",start:700});
	/*Scroll to Top*/
	
	/*Sharing*/
	$('a.share_expand').click(function() {
		if ($('.box_share').is(":hidden")) {
			$('.box_share').show();
		}
		else {
			$('.box_share').hide();
		}
		return false;
	});
	$(".btn_close").click(function() {
		$(".box_share").hide();
		return false;
	});
	$('#zm').popupWindow({
		width:600,
		height:410,
		centerScreen:1
	});
	/*Sharing*/
	
	$(document).click(function(event) {
		if ((event.target.className != 'ajax-checklist-select-value') && (event.target.className != 'name_prd') && (event.target.className != 'price_prd') && (event.target.className != 'ajax-checklist-select-control')) {
			var list1 = $('#ShowResultsList');
			list1.hide();
		}
	});
	$('#ShowResultsChecklist').click(function() {

		var list = $('#ShowResultsList');
		if (list.is(':visible')) {
			list.hide();
		} else {
			list.show();
		}
	});
	
	$(".other_pic").simpleSlider({
		item:8
	});
	
	if ($(".other_pic #next").length)
	{
		$(".other_pic").addClass("hg132");
	}
	
	$('.go-next').click();

	$(".full_description_inside img").lazyload({
		placeholder : imgurl+"/grey.gif"
	});

	$("#main_img").resizeImg({maxHeight: 300, maxWidth: 300});
	
	
	$('#box_thumbnail img').click(function(){
		var path = $(this).attr('src');
		path = path.replace('_130_130','_574_0');
		path = path.replace('t.f','d.f');
		$('#main_img').fadeOut(function(){
			$('#main_img').attr('src', path);
			$('#main_img').fadeIn(function(){
				$("#main_img").resizeImg({maxHeight: 300, maxWidth: 300 });
			});
		});
		return false;
	});
	
	$("#btn_wishlist").click(addWishList);
});

function showproductlist(me_id, topic_id, showResultDiv) {
	var content = jQuery.trim($('#'+showResultDiv).html());
	if (content == '' || content == 'undefined') {
		getproductlist(me_id, topic_id, showResultDiv);
	}
	else {
		//$("#"+showResultDiv).show();
		
	}
}

function getproductlist(me_id, topic_id, showResultDiv) {
	$.get(baseurl+"/product/ajaxproductlist",
		{
			me_id: me_id,
			topic_id: topic_id
			
		},
		function(result){
			var html = '<ul>';
			if (result != null) {
				var data = result.products;
				if (data != null) {
					var objProduct = '';
					var i = 1;
					$.each(data, function(index, value){
						objProduct = value;
						html += '<li ';
						if (objProduct.is_inactived) {
							html += 'class="inactive"';
						}
						html += 'rel="'+objProduct.me_id+'.'+objProduct.topic_id +'.'+objProduct.product_id+'"><span class="name_prd">'+objProduct.product_name+'</span><span class="price_prd">'+objProduct.unit_price+' VNĐ</span></li>';
					});
				}
				else {
					html += '<li> Hiện chưa có sản phẩm nào trong nhóm này</li>';
				}
				html += '</ul>';
				$("#"+showResultDiv).html(html);
				var hoverTimer;

				$('#ShowResultsList ul li').each(function(){
					$(this).mouseover(function() {
						var dom = this;
						if (hoverTimer)
						{
							clearTimeout(hoverTimer);
							hoverTimer = null;
						}
						
						if ($(this).attr('class')!='inactive')
						{
							$(this).css('cursor','pointer');
							$(this).css('background-color','#eef3fc');
						}
						
						hoverTimer = setTimeout(function() {
							var index = $('#ShowResultsList ul li').index(dom);
							$('#box_thumbnail img:eq('+ index +')').click();
						}, 500);
					}).mouseout(function(){
						$(this).css('background-color','');
					}).click(function(){
						if ($(this).attr('class')!='inactive')
						{
							$("#ShowResultsLabel .ajax-checklist-select-control").html($(this).html());
							$("#ShowResultsLabel .ajax-checklist-select-control").attr('rel',$(this).attr('rel'));
						}
					});
				});
				//$("#"+showResultDiv).show();
			}
		},
	"json");
}

// merge from file productdetail
var number_rate = 0;
/**
 * Show Popup rate
 */
function showPopRate(number_rate){
	if(number_rate%4 == 0){
		number_rate = number_rate/4;
	}
	else{
		number_rate = Math.floor((number_rate/4)+1);
	}
	var html =	'<div id="popup" style="width:400px;margin:0 auto">'+
				'<div class="title-popup">Đánh giá sản phẩm<a title="Đóng lại" class="btn-close close"><img src="'+IMAGE_URL+'/boxy/icon_close.jpg" alt="Đóng lại" /></a></div>'+
					'<div class="content-popup" style="position:relative;width:380px">'+
						'<p><span class="label-rating">Đánh giá:</span>';
			for(i = 1; i <= 5; i++){
				if(i == number_rate){
					html +=	'<input name="star1" type="radio" class="star vote_rate" value="'+i+'" checked="checked"/>';
				}else{
					html +=	'<input name="star1" type="radio" class="star vote_rate" value="'+i+'"/>';
				}
			}
							
			html +=			'<br class="clear" />Bình luận về sản phẩm (bắt buộc)<br />'+
							'<textarea class="comment-rating" name="comment-rating" id="msg_content" cols="" rows=""></textarea>'+
							'<input type="hidden" id="popup_rate" name="popup_rate" value=""/>'+
						'</p>'+
						'<div class="clear"></div>'+
					'</div>'+
				    '<div class="footer-popup">'+
				        '<input class="btn-accept" type="button" value="Gửi" onclick="postCommentRate(this);"/>'+
						'<input class="close btn-cancel" style="margin-left:10px" type="button" value="Bỏ qua" />'+
				    '</div>'+
				'</div>';
	var options = 	{
						title: 'Đánh giá sản phẩm',
						type: 'loading',
						modal: true
					};
	new Boxy(html, options);
	$('.vote_rate').rating({
		callback: function(value, link){
			$('#popup_rate').val(value);
		}
	});
	$('#popup_rate').val(number_rate);
}

/**
 * Function allows show popup when user are not sign in
 */
function showPopLogin(){
	var html = '<div id="popup" style="width:400px;margin:0 auto">'+
					'<div class="title-popup">'+
				    	'Đánh giá sản phẩm'+
				        '<a class="btn-close close"><img src="' + IMAGE_URL + '/icon_close.jpg" alt="123mua" /></a>'+
				    '</div>'+
					'<div class="content-popup" style="position:relative;width:380px">'+
						'<p class="row-first-popup"><strong>Bạn cần đăng nhập để đánh giá sản phẩm.</strong>Vui lòng đăng nhập bằng một trong các tài khoản sau:<br  />'+
							'<a class="login-apps login-yahoo" href="javascript://" onclick="openLoginWindow(3,2)">Yahoo</a>'+
							'<a class="login-apps login-google" href="javascript://" onclick="openLoginWindow(2,2)">Google</a>'+
							'<span style="float:left;height:46px;padding:0 5px;line-height:46px">hoặc</span>'+ 
							'<a class="login-apps login-zingme" href="'+LOGIN_URL+'" rel="nofollow">Zing Me</a>'+
						'</p>'+
						'<div class="clear"></div>'+
					'</div>'+
				    '<div class="footer-popup">'+
						'<input class="btn-cancel close" type="button" value="Đóng" />'+
				    '</div>'+
				'</div>';
	var options = 	{
						title: 'Đánh giá sản phẩm',
						type: 'loading',
						modal: true
					};
	new Boxy(html, options);
}
function getRateProduct() {
	$.post(baseurl+"/comment/getrate",
		{
			"topic_id": topic_id,
			"owner_id": topic_me_id
		},
		function(data){
			showRateProduct(data);
		},
	"json");
}
function showRateProduct(data){
	var disabled = "";
	if(ME_ID == shop_me_id){
		disabled = 'disabled="disabled"';
	}
	number_rate = Math.floor((data.avgRate * 4) + 0.5);
	var html = 	'<span class="label">Đánh giá</span>';
	for(i = 1; i <= 20; i++){
		if(i == number_rate){
			html +=	'<input name="star1" type="radio" class="star {split:4} star-show" checked="checked" value="'+i+'" '+disabled+'/>';
		}else{
			html +=	'<input name="star1" type="radio" class="star {split:4} star-show" value="'+i+'" '+disabled+'/>';
		}
	}
	html += '<em>(<span id="total_rate_topic">'+data.sumVoter+'</span> đánh giá)</em>';
	$('#rating_product').html(html);
	$('#rating_product').css('display', 'block');
	$('.star-show').rating({
		callback: function(value, link){
			rateProduct(value);
		}
	});	
}
//Popup rate product
function rateProduct(value){
	if((ME_ID > 0 && ME_ID != shop_me_id) || (VIEW_ID > 0 && (USER_TYPE == 2 || USER_TYPE == 3))){
		showPopRate(value);
		return false;
	}else if(ME_ID == shop_me_id){
		$(this).myBoxy (Boxy,{message: "Bạn không thể tự đánh giá sản phẩm của mình!"});
	}else{
		showPopLogin();
	}
}
function postCommentRate(pop){
	return postComment('msg_content', 'popup_rate', 1, pop);
	
}
//Login qua Google=2, Yahoo=3
function openLoginWindow (foreign_type, type){	
	var loginUrl = '';
	var _width	=	500;
	var Xpos	=	( (screen.availWidth - _width)/2 );
	var _height	=	450;
	var Ypos  	=	( (screen.availHeight - _height)/2 );  
	if(foreign_type == 2){
		loginUrl = baseurl + '/google/request';
	}else if(foreign_type == 3){
		loginUrl = baseurl + '/yahoo/request';
	}else{
		return false;
	}
	window.loginWindow = window.open(loginUrl + '?url_refresh=' + encodeURIComponent(location.href) + '&foreign_type=' + type,'','width='+_width+',height='+_height+',toolbar=no,resizable=fixed,status=no,scrollbars=no,menubar=no,screenX='+Xpos+',screenY='+Ypos);    
	loginWindow.focus();
	return false;
}
function logoutForeign(){
	var loginUrl = '';
	var _width	=	500;
	var Xpos	=	( (screen.availWidth - _width)/2 );
	var _height	=	450;
	var Ypos  	=	( (screen.availHeight - _height)/2 );  
	window.logoutWindow = window.open(baseurl + '/user/logoutforeign?url_refresh=' + encodeURIComponent(location.href),'','width='+_width+',height='+_height+',toolbar=no,resizable=fixed,status=no,scrollbars=no,menubar=no,screenX='+Xpos+',screenY='+Ypos);    
	logoutWindow.focus();
	return false;
}

function deleteComment(commentId) {
	$.get(baseurl+"/comment/delete/appid/0/id/"+topic_id+"/commentId/"+ commentId + "/topic_me_id/" + topic_me_id, function(data){
			getListComment(current_comment_page);
		}, "json");
};

	
function createCommentElement(objComment){
	var html = "";
	var profile_url = '';
	html += '<div class="comment_post clearfix">';
	html += '<div class="closeAll" id="comment_close_'+objComment.ownerId+'_'+objComment.commentId+'">';
	if(objComment.ownerId == VIEW_ID || is_permit==1){
		html += '<a href="javascript:void(0);" style="padding-right: 5px;"><img height="14" border="0" width="14" src="'+IMAGE_URL+'/zme_closeAll2.png" onmouseover="this.src=\''+IMAGE_URL+'/zme_closeAlls.png\'" onmouseout="this.src=\''+IMAGE_URL+'/zme_closeAll2.png\'" onclick="deleteComment('+ objComment.commentId +')" title="Xóa" alt="Xóa"><'+'/a>';
	}
	
	html += '</div>';
	if(2 == objComment.source || 3 == objComment.source){
		if(2 == objComment.source){
			profile_url = 'http://google.com/profiles/' + objComment.fu_id;
		}else{
			profile_url = 'http://pulse.yahoo.com/_' + objComment.fu_id;
		}
		html += '<div class="avatar_min">'+
					'<span><span a_class="" a_width="50" a_height="50" id="ZMEA213">'+
						'<img height="50" width="50" onerror="this.src=&quot;'+objComment.avatar+'&quot;;" src="'+objComment.avatar+'">'+
					'</span></span>'+
				'</div>'+
				'<div class="info_comment">'+
				'<h4><a href="'+profile_url+'" rel="nofollow"><span><span class="">'+objComment.fullname+'</span></span></a></h4>';
	}else{
		html += '<div class="avatar_min"><span rel="ZMEA_'+ objComment.ownerName +'?width=50&amp;height=50"><'+'/span><'+'/div>'+
				'<div class="info_comment">'+
				'<h4><a rel="nofollow" href="http://me.zing.vn/'+ objComment.ownerName +'/profile"><span rel="ZMED_'+ objComment.ownerName +'"><'+'/span><'+'/a><'+'/h4>';
	}
	html += '<span class="update_time">Cách đây '+ objComment.time +'<'+'/span>';
	html += '<p>'+ objComment.content +'<'+'/p>';
	if(objComment.fromId == 1){
		html += '<p style="color:#812b38">Đánh giá: '+objComment.toId+'/5</p>';
	}
	html += '<'+'/div><'+'/div>';

	return html;
};	

function getListComment(page) {
	var mydate = new Date();
	$.get(baseurl+"/comment/getlist/appid/0/id/"+topic_id+"/p/"+ page +"?v="+ mydate.getSeconds(), function(result){
		current_comment_page = page;
		if( result != null ){
			var html = "";
			var objComment;

			var data = result.data;

			if( data != null && data.length > 0 ){
				var paging = result.pager;

				for( var i = 0; i < data.length; i++ ){
					objComment = data[i];
					html += createCommentElement(objComment);
				}
				$("#listComment").html(html);
				$("#paging").html(paging);
				$("#paging").show();
			}else{
				html = '<div class="comment_post clearfix" style="text-align: center;" id="list_comment_empty">Chưa có bình luận.<'+'/div>';
				$("#listComment").html(html);
				$("#paging").hide();
			}
			ZME_widget_mix();
		}
	}, "json");
}


//post comment
/*
* type = 0 // comment thong thuong
* type = 1 // comment danh gia san pham
*/
function postComment(content, rate_field, type, pop){
	var txtContent = $('#'+content).val();
	var appid = 0;
	var rate = '';
	if (type == 1) {
		rate = $("#"+rate_field).val();
		if(0 == rate || '' == rate){
			alert("Bạn chưa chọn đánh giá sản phẩm");
			return false;
		}
	}
	if( jQuery.trim(txtContent) == '' || txtContent =='Bình luận của bạn...' ){
		if(1 == type){
			alert("Bạn vui lòng nhập nội dung cho đánh giá của mình về sản phẩm này.");
		}else{
			$(this).myBoxy (Boxy,{
				message: "Bạn chưa nhập nội dung bình luận!"
			});
		}
		return false;
	}

	$.post(baseurl+"/comment/insertcmproduct",
		{
			"id": ID,
			"content": txtContent,
			"appid": appid,
			"rate": rate,
			"type": type
		},
		function(data){
			if (data != null){
				if(data.error_code == 1){
					alert('Bạn gửi bình luận quá nhanh!');
					return false;
				}else if(data.error_code == 2){
					alert('Bạn đã đánh giá sản phẩm này rồi!');
					return false;
				}else if(data.error_code == 3){
					alert('Bạn không được phép gửi bình luận!');
					$('#'+content).val('');
					return false;
				}else if (data.error_code == 0){
					$('#'+content).val('');
					$('#max_txtcomment_length').html(500);
					html = createCommentElement(data.commentObj);
					$('#listComment').prepend(html);
					$('#list_comment_empty').remove();
					ZME_widget_mix();
					if(1 == type){
						Boxy.get(pop).hide();
						var num_rate_topic = Number($('#total_rate_topic').html());
						$('#total_rate_topic').html((num_rate_topic + 1));
					}
					return false;
				}
			}
		},
	"json");
	return false;
}

function limiter(text_area_id, count_id, max_length){
	var message_desc = $("#"+text_area_id).val();
	var len = message_desc.length;
	if(len > max_length){
		alert("Bạn đã nhập quá số ký tự cho phép (> 500 ký tự)");
		$("#"+count_id).css("color","#C00607");
		$("#"+count_id).html(max_length-len);
		
	} else if (len == max_length){
		$("#"+count_id).css("color","#ACACAC");
		$("#"+count_id).html("0");
	}else{
		$("#"+count_id).css("color","#ACACAC");
		$("#"+count_id).html(max_length-len);	
	}
}
function getEmoticonForm(div_id) {

	var emoticonDiv = '<div class="e1">'+
					    '<table width="450" border="0" cellpadding="0" cellspacing="0">'+
					        '<tbody>'+
					            '<tr>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'1.jpg" onclick="emoticon_msg(\':)\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'2.jpg" onclick="emoticon_msg(\':~\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'3.jpg" onclick="emoticon_msg(\':b\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'4.jpg" onclick="emoticon_msg(\':|\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'5.jpg" onclick="emoticon_msg(\'8-)\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'6.jpg" onclick="emoticon_msg(\':-((\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'7.jpg" onclick="emoticon_msg(\':$\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'8.jpg" onclick="emoticon_msg(\':x\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'9.jpg" onclick="emoticon_msg(\':z\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'10.jpg" onclick="emoticon_msg(\':((\');" style="cursor: pointer;" /></td>'+
					                '<td rowspan="4" align="right" valign="middle"><img src="'+IMAGE_URL+'/emoticon_bar_close.jpg" style="cursor: pointer;" onclick="emoticon_close()" /></td>'+
					            '</tr>'+
					            '<tr>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'11.jpg" onclick="emoticon_msg(\':-|\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'12.jpg" onclick="emoticon_msg(\':@\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'13.jpg" onclick="emoticon_msg(\':p\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'14.jpg" onclick="emoticon_msg(\':d\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'15.jpg" onclick="emoticon_msg(\':o\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'16.jpg" onclick="emoticon_msg(\':(\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'17.jpg" onclick="emoticon_msg(\'--b\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'18.jpg" onclick="emoticon_msg(\':q\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'19.jpg" onclick="emoticon_msg(\':t\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'20.jpg" onclick="emoticon_msg(\';xx\');" style="cursor: pointer;" /></td>'+
					            '</tr>'+
					            '<tr>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'21.jpg" onclick="emoticon_msg(\';p\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'22.jpg" onclick="emoticon_msg(\';-d\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'23.jpg" onclick="emoticon_msg(\';d\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'24.jpg" onclick="emoticon_msg(\';o\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'25.jpg" onclick="emoticon_msg(\';g\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'26.jpg" onclick="emoticon_msg(\'|-)\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'27.jpg" onclick="emoticon_msg(\':!\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'28.jpg" onclick="emoticon_msg(\':l\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'29.jpg" onclick="emoticon_msg(\':>\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'30.jpg" onclick="emoticon_msg(\':;\');" style="cursor: pointer;" /></td>'+
					            '</tr>'+
					            '<tr>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'31.jpg" onclick="emoticon_msg(\';f\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'32.jpg" onclick="emoticon_msg(\';-s\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'33.jpg" onclick="emoticon_msg(\';?\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'34.jpg" onclick="emoticon_msg(\';-x\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'35.jpg" onclick="emoticon_msg(\';@\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'36.jpg" onclick="emoticon_msg(\';8\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'37.jpg" onclick="emoticon_msg(\';!\');" style="cursor: pointer;" /></td>'+
					                '<td><img src="'+PATH_ICON_EMOTI+'38.jpg" onclick="emoticon_msg(\';-!\');" style="cursor: pointer;" /></td>'+
					            '</tr>'+
					        '</tbody>'+
					   '</table>'+
					'</div>';
    $("#"+div_id).html(emoticonDiv);
}
/**
 * get box comment when user has not login
 */
function getUnLoginCommentBox(div_id) {
	$("#errorComment").attr("class", "errorComment");
	var html = 	'<p class="no_avatar"><img src="'+IMAGE_URL+'/icon_no_avatar.jpg" width="54" height="54" alt="no avatar" /></p>'+
					'<p class="desc_no_avatar"><strong>Đăng nhập để đặt câu hỏi và đánh giá về topic</strong>'+
					'<br  />'+
					'<a class="login_apps login_yahoo" href="javascript:void(0);" onclick="openLoginWindow(3,1)">Yahoo</a>'+
					'<a class="login_apps login_google" href="javascript:void(0);" onclick="openLoginWindow(2,1)">Google</a><span class="fl">hoặc</span>'+ 
					'<a class="login_apps login_zingme" href="'+LOGIN_URL+'" rel="nofollow">Zing Me</a>'+
				'</p>';
	$("#"+div_id).html(html);
}

/**
 * get box comment when user has logged
 */
function getLoggedCommentBox(div_id, user_avatar, user_fullname, user_email, user_url) {
	$("#errorComment").attr("class", "");
	if (ME_ID > 0) {
		avatar = '<span rel="ZMEA_'+ME_UNAME+'" width=50&amp;height=50"></span>';
		user_url = "";
		logout_url = "";
	}
	else {
		avatar = '<a href="'+user_url+'"><img src="'+user_avatar+'" alt="123mua" width=50 height=50 /></a>';
		user_url = "";
		logout_url = '<a class="logoff" onclick="logoutForeign()" href="javascript://" style="text-decoration: none;">(Thoát)</a>';
	}
	if (user_email != '') {
		user_email = '('+user_email+')';
	}
	var emoticon_div = '<div id="emoticon" style="display:none;"></div>';
	var emoticon_lose = '<div id="emoticon_lose" style="display:block;">'+
							'<div class="e1">'+
								'<table border="0" cellpadding="0" cellspacing="0">'+
									'<tbody>'+
										'<tr>'+
											'<td><img src="'+PATH_ICON_EMOTI+'1.jpg" onClick="emoticon_open()" style="cursor: pointer;" /></td>'+
										'</tr>'+
									'</tbody>'+
								'</table>'+
							'</div>'+
						'</div>';
	
	var comment_form = 
					'<div class="typing_row">'+
						'<div class="type_left">'+avatar+'</div>'+
						'<div class="type_right">'+
							'<p class="user_name_login">'+user_fullname+user_email+logout_url+'</p>'+
							'<p class="character_login">Ký tự cho phép: <span id="max_txtcomment_length"> 500</span> ký tự</p>'+
							'<textarea id="txtcomment" name="txtcomment" onkeyup="limiter(\'txtcomment\', \'max_txtcomment_length\', 500);" onfocus="clearText();" onblur="rollbackComment();">Bình luận của bạn...</textarea>'+
						'</div>'+
						'<div class="clear"></div>'+
					'</div>'+
					'<div class="typing_row">'+
						'<div class="type_right"><input id="btnComment" class="submitbutton" name="commit" type="submit" value="" /></div>'+
						'<div class="clear"></div>'+
					'</div>';
	var html = emoticon_div + emoticon_lose + comment_form;
	$("#"+div_id).html(html);
	getEmoticonForm('emoticon');
	
	$('#btnComment').click(function() {
		postComment('txtcomment', '', 0, '');
	});
}	

function loadCommentBox() {
	if (ME_ID > 0) {
		getLoggedCommentBox('post_comment', ME_UNAME, ME_UNAME, ME_UNAME+'@zing.vn', 'http://me.zing.vn/'+ME_UNAME);
	} else {
		$.get( baseurl+"/comment/getuserinfo", 
				function(data){
					if (data == null || data.is_logged == false) {
						getUnLoginCommentBox('errorComment');
					} else {
						getLoggedCommentBox('post_comment', data.user_info.avatar, data.user_info.fullname, data.user_info.email, data.user_info.user_url);
						VIEW_ID = data.user_info.user_id;
						USER_TYPE = data.user_info.user_type;
						addCloseComment();
						if(data.is_focus){
							$('html, body').animate({
			                     scrollTop: 30000
			                 },1500);
			                 $('#txtcomment').focus();
			             }
					}
				}, "json");
	}
}
function addCloseComment(){
	$("div[id^='comment_close_']").each(function (i) {
        var arrId = this.id.split('_');
		var uid = arrId[2];
		var cid = arrId[3];
		if(uid == VIEW_ID){
			var html = '<a href="javascript:void(0);" style="padding-right: 5px;"><img height="14" border="0" width="14" src="'+IMAGE_URL+'/zme_closeAll2.png" onmouseover="this.src=\''+IMAGE_URL+'/zme_closeAlls.png\'" onmouseout="this.src=\''+IMAGE_URL+'/zme_closeAll2.png\'" onclick="deleteComment('+ cid +')" title="Xóa" alt="Xóa"></a>';
			$('#' + this.id).html(html);
		}
    });
}
